import { getToken, getUser, removeToken, removeUser, setToken, setUser } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: JSON.parse(getUser())
  },
  mutations: {
    SETTOKEN(state, data) {
      state.token = data.token.token
      state.userInfo = data.user
      setToken(data.token.token)
      setUser(JSON.stringify(data.user))
    },
    REMOVETOKEN(state) {
      removeToken()
      state.token = null
    },
    REMOVEUSER(state) {
      removeUser()
      state.userInfo = {}
    }
  },
  actions: {
    logout(ctx) {
      ctx.commit('REMOVETOKEN')
      ctx.commit('REMOVEUSER')
    }
  }
}
